package com.codingstudy.login.controller;

import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.codingstudy.login.components.*;
import com.codingstudy.login.entity.SysRoleTable;
import com.codingstudy.login.entity.SysUserEntity;
import com.codingstudy.login.service.SysRoleTableService;
import com.codingstudy.login.service.SysUserService;
import com.codingstudy.login.vo.SysUserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.ir.CallNode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 登录接口
 */
@RestController
@RequestMapping("user")
@Api(description = "用户表")
public class SysUserController extends ApiController {

    @Autowired
    SysUserService userService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
//    角色
    @Autowired
    private SysRoleTableService sysRoleTableService;

    private String header = "Authorization";
    /**
     * 查询所有数据集
     * @return
     */
    @ApiOperation(value = "查询所有数据集",response= SysUserEntity.class)
    @GetMapping("search")
    public Result getList() {

        List<SysUserEntity> list =  userService.list();
        return  new Result(true, StatusCode.OK,"查询成功",list);
    }

    /**
     * 只要用户登录了才可以查看
     * @return
     */
    @GetMapping("finbyuser")
    @ApiOperation(value = "获取用户信息",response= SysUserEntity.class)
    public Result getUserByUserName(HttpServletRequest request) {
        String username = login.getusername(request,jwtTokenUtil);
        if (StringUtils.isEmpty(username)){
            return new Result(false,StatusCode.ERROR,"用户未登录");
        }
        SysUserEntity sysUserEntity = userService.getUserByUserName(username);
        sysUserEntity.setPassWord(null);
        SysUserVo sysUserVo = new SysUserVo();
        //拷贝到返还对象中
        BeanUtils.copyProperties(sysUserEntity, sysUserVo);

        //查询角色
        List<String> roles = sysRoleTableService.getRolesByUserName(username);
        sysUserVo.setRoles(roles);
        return  new Result(true, StatusCode.OK,"用户信息返回成功",sysUserVo);
    }

    /**
     * 添加用户、用户自行注册。
     * @param userVo
     * @return
     */
    @PostMapping("register")
    @ApiOperation(value = "添加用户",response= SysUserEntity.class)
    public Result register(@RequestBody(required = false) SysUserVo userVo) {
       try {
           return  new Result(true, StatusCode.OK,"添加成功");
       }catch (Exception e){
           return  new Result(false, StatusCode.ERROR,"添加失败");
       }
    }

    /**
     * 修改数据
     *
     * @param sysUserVo 实体对象
     * @return 修改结果
     */
    @PutMapping
    @ApiOperation(value = "修改用户数据",response= SysUserEntity.class)
    public Result update(@RequestBody SysUserVo sysUserVo) {
        SysUserEntity po = this.userService.voToPo(sysUserVo);
        System.out.println("po = " + po);
        this.userService.updateById(po);
        return  new Result(true, StatusCode.OK,"修改成功");
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @ApiOperation(value = "删除用户数据",response= SysUserEntity.class)
    @DeleteMapping
    public Result delete(@RequestParam("idList") List<String> idList) {
        this.userService.removeByIds(idList);
        return  new Result(true, StatusCode.OK,"删除成功");
    }

}
